package com.wxy.CGLib;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CGlibProxy {
	public static void main(String[] args) {
		final userDaoImpl userDaoImpl=new userDaoImpl();
		final Advice advice=new Advice();
		//创建增强器
		Enhancer enhancer=new Enhancer();
		//设置父类
		enhancer.setSuperclass(userDaoImpl.getClass());
		//生成代理方法
		enhancer.setCallback(new MethodInterceptor() {
			
			@Override
			public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
				// TODO 自动生成的方法存根
				advice.before();
				proxy.invoke(userDaoImpl, args);
				advice.after();
				return null;
			}
		});
		userDaoImpl userDaoImpl2=(com.wxy.CGLib.userDaoImpl) enhancer.create();
		userDaoImpl2.addUser();
	}
}
